package tbmanager;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;

import student.Course;
import student.Score;
import student.Student;
import util.HibernateUtil;

/**
 * Servlet implementation class ProcessData
 */
@WebServlet("/ProcessData")
public class ProcessData extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProcessData() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		Integer sid=Integer.valueOf(request.getParameter("sid"));
		String cname=request.getParameter("cname");
		Integer cscore=Integer.valueOf(request.getParameter("cscore"));
		Session session=HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Student stu=session.get(Student.class, sid);
		
		session=HibernateUtil.getSessionFactory().getCurrentSession();
		String hql="from Course where name='"+cname+"'";
		System.out.println(hql);
		Query query=session.createQuery(hql);
		Course cou=(Course) query.list().get(0);
		
		session=HibernateUtil.getSessionFactory().getCurrentSession();
		Score sc=new Score();
		sc.setCourse(cou);
		sc.setStudent(stu);
		sc.setScore(cscore);
		session.save(sc);
		session.getTransaction().commit();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
